<?php


namespace App\Http\Controllers\Customer;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\RejectRequest;
use App\Http\Requests\Customer\StoreRequest;
use App\Http\Requests\Customer\UpdateRequest;
use App\Http\Searchs\Customer\IndexSearch;
use App\Repositories\Customer;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Customer
     * @author luffyzhao@vip.126.com
     */
    private $customer;

    /**
     * IndexController constructor.
     * @param Customer $customer
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Customer $customer)
    {
        $this->customer = $customer;
    }

    /**
     * 客户列表
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->customer->paginate(
                $search->toArray()
            )
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->customer->create(
                    $request->only(['name', 'phone', 'assure', 'contacts', 'remark', 'docking'])
                );
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * @author luffyzhao@vip.126.com
     */
    public function destroy($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->delete($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->customer->edit($id)
        );
    }

    /**
     * @param $id
     * @param UpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update($id, UpdateRequest $request)
    {
        return $this->response(
            $this->customer->update(
                $id,
                $request->only(['name', 'phone', 'assure', 'contacts', 'remark'])
            )
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function show($id)
    {
        return $this->response(
            $this->customer->show($id)
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function submit($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->submit($id);
            })
        );
    }


    /**
     * @param $id
     * @param RejectRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function reject($id, RejectRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->customer->reject($id, $request->input('value'));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function adopt($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->adopt($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function frozen($id, RejectRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->customer->frozen($id, $request->input('value'));
            })
        );
    }

    /**
     * @param $id
     * @param RejectRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function unfrozen($id, RejectRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->customer->unfrozen($id, $request->input('value'));
            })
        );
    }
}
